#include "convert.h"
namespace berry{

int Convert::hexStringToInt(char c){
    if(c>='0'&&c<='9') return(c-'0');
    if(c>='A'&&c<='Z') return(c-'A'+10);
    if(c>='a'&&c<='z') return(c-'a'+10);
    return 0;
}

char* Convert::hexStringToByteArray(std::string str){
    int sz = str.length();
    char* bytes = new char[sz/2];
    for(int i=0;i<sz;i+=2){
        bytes[i/2] = (char)((hexStringToInt(str.at(i)) << 4)|hexStringToInt(str.at(i+1)));
    }
    return bytes;
}

std::string Convert::byteArrayToHexStrng(char* bytes,int len){
    std::string str("");
    std::string hexStr("0123456789abcdef");
    for(int i=0;i<len;i++){
        int b = 0x0f & (bytes[i]>>4);
        str.append(1,hexStr.at(b));
        b = 0x0f & bytes[i];
        str.append(1,hexStr.at(b));
    }
    return str;
}
}
